﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace DataAccess
{
    public class DAUsers
    {
        /// <summary>
        /// Insert new user in the db
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public bool Insert(ref User user)
        {
            try
            {
                using (var db = new DBWSSDataContext())
                {
                    db.Users.InsertOnSubmit(user);
                    db.SubmitChanges();
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// Check username
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public bool CheckUsername(string username)
        {
            try
            {
                using (var db = new DBWSSDataContext())
                {
                    var temp = db.Users.FirstOrDefault(o => o.Username.Trim().ToLower() == username.Trim().ToLower());
                    return temp == null ? true : false;
                }
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// Check email
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public bool CheckEmail(string email)
        {
            try
            {
                using (var db = new DBWSSDataContext())
                {
                    var temp = db.Users.FirstOrDefault(o => o.Email.Trim().ToLower() == email.Trim().ToLower());
                    return temp == null ? true : false;
                }
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// Update user info
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public bool Update(User user)
        {
            try
            {
                using (var db = new DBWSSDataContext())
                {
                    var temp = db.Users.FirstOrDefault(o => o.Id == user.Id);
                    if (temp == null)
                        return false;
                    temp.City = user.City;
                    temp.CreditCardNo = user.CreditCardNo;
                    temp.CVS = user.CVS;
                    temp.Email = user.Email;
                    temp.ExpDate = user.ExpDate;
                    temp.Firstname = user.Firstname;
                    temp.Lastname = user.Lastname;
                    temp.Password = user.Password == null ? temp.Password : user.Password;
                    temp.State = user.State;
                    temp.Street = user.Street;
                    //temp.Username = user.Username;
                    temp.Zipcode = user.Zipcode;
                    db.SubmitChanges();
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }

        public List<User> GetUsers(int pageIndex, int pageSize, out int totalPages)
        {
            totalPages = 0;
            try
            {
                int startIndex = pageIndex * pageSize;
                using (var db = new DBWSSDataContext())
                {
                    IQueryable<User> temp = db.Users;
                    if (temp.Count() == 0) return null;
                    temp = temp.OrderBy(o => o.Username).Skip(startIndex).Take(pageSize);
                    int totalRecords = temp.Count();
                    totalPages = totalRecords / pageSize * pageSize == totalRecords ? totalRecords / pageSize : totalRecords / pageSize + 1;
                    return temp.ToList();
                }
            }
            catch
            {
                return null;
            }
        }
        /// <summary>
        /// Get a user
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public User GetUser(int userId)
        {
            try
            {
                using (var db = new DBWSSDataContext())
                {
                    return db.Users.FirstOrDefault(o => o.Id == userId);
                }
            }
            catch
            {
                return null;
            }
        }
        /// <summary>
        /// Remove a user
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public bool Delete(int userId)
        {
            try
            {
                using (var db = new DBWSSDataContext())
                {
                    var temp = db.Users.FirstOrDefault(o => o.Id == userId);
                    if (temp == null) return false;
                    db.Users.DeleteOnSubmit(temp);
                    db.SubmitChanges();
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// Get user
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public User GetUser(string username, string password)
        {
            try
            {
                using (var db = new DBWSSDataContext())
                {
                    return db.Users.FirstOrDefault(o => o.Username == username&&o.Password==password);                  
                }
            }
            catch
            {
                return null;
            }
        }
    }
}